/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.tutotec.utils;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.Random;
import javax.imageio.ImageIO;

/**
 *
 * @author Acacio Salgueiro
 */
public class ImageDataBase64UrlDecode {

    private final String dataBase64Url;
    private InputStream imageInputStream;
    private TiposImg tipoImg;
    
    private enum TiposImg{NONE, PNG, JPG, GIF}

    public ImageDataBase64UrlDecode(String dataBase64Url) {
        this.dataBase64Url = dataBase64Url;
        Decode();
    }
    
    private void detectaTipoImagem(){
        
        if(dataBase64Url.toLowerCase().indexOf("image/png")>=0)
            tipoImg = TiposImg.PNG;
        else
        if(dataBase64Url.toLowerCase().indexOf("image/jpg")>=0)
            tipoImg = TiposImg.JPG;
        else
        if(dataBase64Url.toLowerCase().indexOf("image/gif")>=0)
            tipoImg = TiposImg.GIF;
        else
            tipoImg = TiposImg.NONE;
        
        
    }
    
    public void Decode(){
        
        detectaTipoImagem();
        
        String data64 = EstractBase64Data(dataBase64Url);
        
        byte[] decodificados = DecodeFromBase64(data64);
                
        imageInputStream = bytes2InputStream(decodificados);
       
    }
    
    private String EstractBase64Data(String data){
        
        return data.substring(data.indexOf(",")+1);
        
    }
    
    private byte[] DecodeFromBase64(String data){
        
        return Base64.getDecoder().decode(data);
        
    }
    
    private InputStream bytes2InputStream(byte[] data){
        return new ByteArrayInputStream(data);
    }
    
    public void saveToFile(String outputFilePath) throws IOException{
        
        File outputFile = new File(outputFilePath);
        
        BufferedImage bufferedImage = ImageIO.read(imageInputStream);
        switch(tipoImg){
            case GIF:
                ImageIO.write(bufferedImage, "gif", outputFile);
                break;
            case JPG:
                ImageIO.write(bufferedImage, "jpg", outputFile);
                break;
            case PNG:
                ImageIO.write(bufferedImage, "png", outputFile);
                break;
            default:
        }
                
        
    }
    
    public File saveToTMPFile() throws IOException{
        Random r = new Random(System.currentTimeMillis());
        File outputFile=null;
        BufferedImage bufferedImage = ImageIO.read(imageInputStream);
        switch(tipoImg){
            case GIF:
                outputFile = File.createTempFile(""+r.nextInt(), ".gif");
                ImageIO.write(bufferedImage, "gif", outputFile);
                break;
            default:
            case JPG:
                outputFile = File.createTempFile(""+r.nextInt(), ".jpg");
                ImageIO.write(bufferedImage, "jpg", outputFile);
                break;
            case PNG:
                outputFile = File.createTempFile(""+r.nextInt(), ".png");
                ImageIO.write(bufferedImage, "png", outputFile);
                break;
        }
        outputFile.deleteOnExit();
        return outputFile;
    }
    
    public static ImageDataBase64UrlDecode getFactory(String dataBase64Url){
        return new ImageDataBase64UrlDecode(dataBase64Url);
    }
    
    
    
}
